<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>        
<script src="/whereismyteacher-c/WebContent/js/jquery-1.9.1.js"></script>
<script src="/whereismyteacher-c/WebContent/js/jquery-ui-1.10.3.custom.js"></script>
<script type="text/javascript">	
	$(document).ready(function(){
		//마우스 위치 저장할 변수
//		var x = null; 
//		var y = null; 
			// 검색 조건의 값을 저장하는 변수
			var total_sido="";
			var total_gugun="";
			var total_name="";
			var total_grade="";
			var total_gender="";
			var total_hopesubject="";
		//초기에 화면에 뿌려줄 내용
		$("#total_gugunDiv").hide();
		$.ajax({
			type:"POST",
			url:"hopeSubjectList.do",
			dataType:"json",
			success:function(jsonData){	
				//시도 리스트 가져와서 뿌리기
				$("#total_sido option").remove();
				var op="<option value='false'>--</option>";
				$("#total_sido").append(op);
 				$.each(jsonData.sidoList,function(index, sidolist){	//jsonData.sidoList을 sidolist란 이름으로 사용
 					op="<option value="+sidolist.code_code+">"+sidolist.code_name+"</option>";
					$("#total_sido").append(op); 
				});

 				//희망과목 가져와서 뿌리기
				$("#total_hopesubject option").remove();
				op="<option value=''>--</option>";
				$("#total_hopesubject").append(op);
 				$.each(jsonData.hopeSubjectList,function(index,hopesubjectlist){
 					op="<option value="+hopesubjectlist.code_code+">"+hopesubjectlist.code_name+"</option>";
					$("#total_hopesubject").append(op); 
				});
 				//성별 가져와서 뿌리기
				$("#total_gender option").remove();
				op="<option value=''>--</option>";
				$("#total_gender").append(op);
 				$.each(jsonData.genderList,function(index, genderlist){
 					op="<option value="+genderlist.code_code+">"+genderlist.code_name+"</option>";
					$("#total_gender").append(op); 
				});
 				//학교학년 가져와서 뿌리기
				$("#total_grade option").remove();
				op="<option value=''>--</option>";
				$("#total_grade").append(op);
 				$.each(jsonData.gradeList,function(index, gradelist){
 					op="<option value="+gradelist.code_code+">"+gradelist.code_name+"</option>";
					$("#total_grade").append(op); 
				});
			}//success
		});//ajax

		$("#total_sido").change(function(){
			total_sido=$(this).val();
			if(total_sido=='false'){//시도 값이 바뀌었는데 --을 선택한 경우
				$("#total_gugunDiv").hide();
			}else{						
				$("#total_gugunDiv").show();				
			}			
			$.ajax({
				type:"POST",
				url:"gugunList.do",
				data:"code_code="+total_sido,
				dataType:"json",
				success:function(jsonData){
	 				//구군정보 가져와서 뿌리기
					$("#total_gugun option").remove();
					var op="<option value=''>--</option>";
					$("#total_gugun").append(op);
	 				$.each(jsonData,function(index, codemaster){
	 					op="<option value="+codemaster.code_code+">"+codemaster.code_name+"</option>";
						$("#total_gugun").append(op); 
					});
				}
			}); 
		});
		
		$("#total_searchBtn").click(function(){		//검색버튼을 클릭하면
			total_sido=$("#total_sido").val();
			total_gugun=$("#total_gugun").val();
			total_name=$("#total_name").val();
			total_grade=$("#total_grade").val();
			total_gender=$("#total_gender").val();
			total_hopesubject=$("#total_hopesubject").val();
			
	//		alert("시도:"+total_sido+"구군"+total_gugun+"이름"+total_name+"학교"+total_grade+"성별"+total_gender+"과목"+total_hopesubject);
			if(total_sido=="false" && (total_gugun==null || total_gugun=="") && total_grade=="" && total_gender=="" && total_hopesubject=="" && total_name==""){
				alert("검색조건을 선택하세요.");
			}else{	//조건에 따른 검색
				$.ajax({
					type:"POST",
					url:"getSearchStudentbyTotal.do",
					data:"total_gugun="+total_gugun+"&total_name="+total_name+"&total_grade="+total_grade+"&total_gender="+total_gender+"&total_hopesubject="+total_hopesubject,
					dataType:"json",
					success:function(jsonData){
						var member_table="";
						alert("검색조건 성공!!"+jsonData.list.length);
						if(jsonData.list.length>=1){	//검색결과 1명이상이면
							$("#total_totalstudent").text("검색결과 총 학생 수 : "+jsonData.list.length);
							$("#listbytotal tbody").text("");	//기존의 내용을 지운다.
	 		 				$.each(jsonData.list,function(index, mvo){
	 		 					member_table  = "<tr><td align='center'>"+mvo.name +"</td>"+
	 		 													"<td align='center'>"+mvo.grade +"</td>"+
	 		 													"<td align='center'>"+mvo.gender +"</td>"+
	 		 													"<td align='center'>"+mvo.hoperegion +"</td>"+
	 		 													"<td align='center'>"+mvo.hopesubject +"</td></tr>";
	 							$("#total_memberlist_Tbody").append(member_table);				
							});
	 		 				$("#total_name").val("");		//검색완료 후 초기화(이름)
							$("#total_grade").val("");	//검색완료 후 초기화(학년셀렉트박스)
							$("#total_gender").val("");		//검색완료 후 초기화(성별)
							$("#total_hopesubject").val("");	//검색완료 후 초기화(희망과목셀렉트박스)
							
						}else{	//검색결과가 0명이면
							alert("검색조건에 맞는 학생이 없습니다.");
						}						
					}
				});			//ajax	
			}	//if
		});	//click
	    $( "#dialog_studendInfo" ).dialog({
	        autoOpen: false,
	        show: {effect: "blind", duration: 100},
	        hide: {effect: "explode", duration: 100}
//	        position: [x,y+10]
	      });
		//학생 이름 클릭되면
 		$("a[id=studentName]").click(function(e){ 
//			alert($(this).next().val());
//			x = e.clientX; y = e.clientY;		//마우스 클릭된 위치값 저장

			var studentId=$(this).next().val();
			var information="";
			$.ajax({
				type:"POST",
				url:"getStudentInfobyId.do",
				data:"studentId="+studentId,  //	data:$("#tradeForm").serialize(),
				dataType:"json",
				success:function(jsonData){
//					alert("success : "+jsonData.name);
					$("#p_studendInfo").text("");	//기존의 내용을 지운다.
					information  = '이름 : '+jsonData.name+'<br>'+
										  '과외지역 : '+jsonData.hoperegion+'<br>'+
										  '과외과목 : '+jsonData.hopesubject+'<br>'+
										  '학교학년 : '+jsonData.grade+'<br>'+
										  '전화번호 : '+jsonData.tel+'<br>'+
										  '이메일 : '+jsonData.email+'<br>'+
										  '주소 : '+jsonData.codeaddress+jsonData.subaddress+'<br>';	 													
 					$("#dialog_studendInfo	").html(information);
 					$( "#dialog_studendInfo" ).dialog( "open" );
				}	//success
			});	//ajax 			
		});
		//페이지 번호 클릭시
		$("a[id=pageList]").click(function(){
			var pageTable="";
			var pageNo=$(this).text();
			if(total_sido=="" && (total_gugun==null || total_gugun=="") && total_grade=="" && total_gender=="" && total_hopesubject=="" && total_name==""){
				//검색조건없이 현재탭의 페이지를 이동
//				alert("검색조건없이로 : "+$(this).text());
				$.ajax({
					type:"POST",
					url:"tabStudentTotalViewPageN.do", // tab 클릭했을 때 실행하는 메서드와 동일한데 리턴값다르게
					data:"pageNo="+pageNo,  //	data:$("#tradeForm").serialize(),
					dataType:"json",
					success:function(jsonData){
						$("#total_totalstudent").text("검색결과 총 학생 수 : "+jsonData.list.length);
						$("#listbytotal tbody").text("");	//기존의 내용을 지운다.
	 		 			$.each(jsonData.list,function(index, mvo){
	 		 				member_table  = "<tr><td align='center'>"+mvo.name +"</td>"+
	 		 												"<td align='center'>"+mvo.hoperegion +"</td>"+
	 		 												"<td align='center'>"+mvo.grade +"</td>"+
	 		 												"<td align='center'>"+mvo.gender +"</td>"+
	 		 												"<td align='center'>"+mvo.hopesubject +"</td></tr>";	 													
	 						$("#total_memberlist_Tbody").append(member_table); 						
						});	//each

						//페이징처리시작
						//	alert("현재페이지 :"+jsonData.pagingBean.nowPage);
						$("#pagingView").text("");	//기존의 내용을 지운다.
						if(jsonData.pagingBean.previousPageGroup){
							pageTable="<a href='tabStudentTotalView.do?pageNo='"+(jsonData.pagingBean.startPageOfPageGroup-1) +"id='prePage' >이전 </a>";
						}
						for( i=jsonData.pagingBean.startPageOfPageGroup; i<=jsonData.pagingBean.endPageOfPageGroup; i++){
							if(jsonData.pagingBean.nowPage !=i){
								pageTable+="<a id='pageList'>"+ i +"</a>";
							}else{
								pageTable+=i;	//링크없이 페이지번호만 표시
							}
						}//for
						if(jsonData.pagingBean.nextPageGroup){
							pageTable+="<a href='tabStudentTotalView.do?pageNo='"+(jsonData.pagingBean.endPageOfPageGroup+1) +" id='postPage' >	다음 </a>";
						}							
						$("#pagingView").append(pageTable); 		
						//페이징처리 끝
					}	//success
				});	//ajax	
			}else{	//검색조건에 따른 페이지 이동
				$.ajax({
					type:"POST",
					url:"getSearchStudentbyTotal.do",
					data:"total_gugun="+total_gugun+"&total_name="+total_name+"&total_grade="+total_grade+"&total_gender="+total_gender+"&total_hopesubject="+total_hopesubject,
					dataType:"json",
					success:function(jsonData){
						$("#total_totalstudent").text("검색결과 총 학생 수 : "+jsonData.list.length);
						$("#listbytotal tbody").text("");	//기존의 내용을 지운다.
		 				$.each(jsonData.list,function(index, mvo){
		 					member_table  = "<tr><td align='center'>"+mvo.name +"</td>"+
		 													"<td align='center'>"+mvo.hoperegion +"</td>"+
		 													"<td align='center'>"+mvo.grade +"</td>"+
		 													"<td align='center'>"+mvo.gender +"</td>"+
		 													"<td align='center'>"+mvo.hopesubject +"</td></tr>";	 													
							$("#total_memberlist_Tbody").append(member_table);				
						});
						alert("이전페이지 : "+jsonData.pagingBean.previousPageGroup);
						alert("현재페이지 : "+jsonData.pagingBean.nowPage);
						alert("다음페이지 : "+jsonData.pagingBean.nextPageGroup);
					}
				});	//ajax	
			}	//if else
		});			
	});
</script>

<form id=total_searchForm>
<table width="560"  cellpadding="2" cellspacing="0"><tr><td>
	<table>
		<tr>
			<td>희망지역</td>
			<td ><select id = "total_sido"></select></td>
			<td colspan="2"> <span id="total_gugunDiv" ><select id="total_gugun"><option value=''>--</option></select></span></td>
			<td>이름</td><td><input type="text" id="total_name"></td>
		</tr>
		<tr>
			<td>학년</td><td><select id="total_grade"></select></td>		
			<td>성별</td><td><select id="total_gender"></select></td>
			<td>희망과목</td><td><select id="total_hopesubject"></select></td>	
			<td align="right"><input type="button" id="total_searchBtn" value="검색"> </td>		
		</tr>
	</table>
</td></tr></table>	
</form>
<table width="560" cellpadding="2" cellspacing="0"><tr align="right"><td>
<div id="total_totalstudent">총 학생 수 : ${requestScope.listVO.pagingBean.totalContent}</div>
</td></tr></table>	
<table id="listbytotal"  width="560"  border="1" cellpadding="2" cellspacing="0">
	<thead>
		<tr><td width="20%" align="center">이름</td>
		 	  <td width="20%" align="center">학교학년</td>
			 <td width="20%" align="center">성별</td>
			 <td width="20%" align="center">희망지역</td>
			 <td width="20%" align="center">희망과목</td>			
		</tr>
	</thead>
	<tbody id="total_memberlist_Tbody">
			<c:forEach var="mvo" items="${requestScope.listVO.list}">		
			<tr>
			    <td align='center'><a href="#" id="studentName">${mvo.name }</a>
			    <input type="hidden" id="hidden_mvo" value="${mvo.id }"></td>
			    <td>${mvo.grade}</td>	
				<td>${mvo.gender}</td>
				<td>${mvo.hoperegion}</td>
				<td>${mvo.hopesubject}</td>				
			</tr>
			</c:forEach>	
	</tbody>	
</table><br>

<div id="dialog_studendInfo" title="개인정보">
<!-- 	 <p id="p_studentInfo">개인정보</p> -->
</div>

<table width="560"  cellpadding="2" cellspacing="0"><tr align="center"><td>
	<div id="pagingView">
<c:set var="pb" value="${requestScope.listVO.pagingBean}"></c:set>
<c:if test="${pb.previousPageGroup}">
	<a href="studentListBasic.do?pageNo=${pb.startPageOfPageGroup-1 }">	Pre </a>
</c:if>	

<c:forEach var="pageNo" begin="${pb.startPageOfPageGroup }" 
										end="${pb.endPageOfPageGroup }">
<c:choose>
<c:when test="${pb.nowPage !=pageNo }" >
	<a id="pageList">${pageNo }</a>
</c:when>
<c:otherwise>
	${pageNo} 
</c:otherwise>
</c:choose>
</c:forEach>

<c:if test="${pb.nextPageGroup}">
	<a href="studentListBasic.do?pageNo=${pb.endPageOfPageGroup+1 }">Next </a>
</c:if>
</div>
</td></tr></table>	
<br>
